<?php 
namespace Admin\Controller;
use \Think\Controller;

class BrandController extends Controller
{		
		protected $brandModel = null;

		public function __construct(){

			parent::__construct();
			$this->brandModel = D('Brand');
		}
		public function brand_add()
		{	
		if(IS_POST){

			if($this->brandModel->create(I('post.')))
			{


				//调用模型
				if($this->brandModel->add())
				{

					$this->success('添加成功',U('brand_list'));
					exit;
				}

				$this->error($this->brandModel->getError());
				exit;
				
			}

				$this->error($this->brandModel->getError());
		
		}

		
		$brandlist = $this->brandModel->getBrand();
		$this->assign('brandlist',$brandlist);

		$this->display();
		}
		//商品编辑
		public function brand_edit()
		{	
			if(IS_POST){

			if($this->brandModel->create(I('post.'),2))
			{

				//调用模型
				if($this->brandModel->save())
				{

					$this->success('修改成功',U('brand_list'));
					exit;
				}

				$this->error($this->brandModel->getError());
				exit;
				
			}

				$this->error($this->brandModel->getError());
		
		}
			
			$info = $this->brandModel->find(I('get.brand_id'));
			$this->assign('brand_info',$info);
			$this->display();
		}
		
		//商品展示
		public function brand_list()
		{	
			$where = array();
			$brand_name = I('get.brand_name');
			if(!empty($brand_name))
				$where['brand_name'] = array('LIKE',"%$brand_name%");

			$count = $this->brandModel->where($where)->count();// 查询满足要求的总记录数
			$Page  = new \Think\Page($count,2);// 实例化分页类 传入总记录数和每页显示的记录数(25)
			$Page->setConfig('next','下一页');
			$Page->setConfig('end','最后一页');
			$Page->setConfig('prev','上一页');
			$show  = $Page->show();// 分页显示输出
			// 进行分页数据查询 注意limit方法的参数要使用Page类的属性
			$list = $this->brandModel->where($where)->limit($Page->firstRow.','.$Page->listRows)->select();
			$this->assign('brandlist',$list);// 赋值数据集
			$this->assign('page',$show);// 赋值分页输出
			$this->display();
		}

		public function brand_delete()
		{
	
		//$logo = $this->brandModel->field('brand_logo')->find(I('get.brand_id'));
		
		//$rp = C('IMG_RPATH');
		//unlink($rp.$logo['brand_logo']);
		
		$info = $this->brandModel->delete(I('get.brand_id'));
			if($info)
			{
				//echo '删除成功';
				$str ='删除成功';
				$this->ajaxReturn($str,'json');
			}
			else
			{
				echo '删除成功';
			}
		}
}

 ?>